package com.zhang.cloudalibabaconsumer8084.controller;

import com.zhang.cloudalibabacommons.entity.JsonResult;
import com.zhang.cloudalibabaconsumer8084.service.FeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class DemoController {
	//服务提供者URL
	@Value("${service-url.nacos-user-service}")
	private String SERVICE_URL;

	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private FeignService feignService;

	@GetMapping("/consumer/fallback/{id}")
	public JsonResult<String> fallback(@PathVariable Long id){
		//通过Ribbon发起远程访问，访问9003/9004
		JsonResult<String> result = restTemplate.getForObject(SERVICE_URL+"/info/"+id,JsonResult.class);
		return result;
	}

	@GetMapping("getInfo/{id}")
	public JsonResult<String> getInfo(@PathVariable("id") Long id){
		if(id > 3){
			throw new RuntimeException("没有该id");
		}
		return feignService.msbSql(id);
	}
}